﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Kaidou.Model;
using Kaidou.Persister;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using Kaidou.Util;
using Kaidou.Bll;
using System.Text.RegularExpressions;


public partial class seal_seal_report_search : System.Web.UI.Page
{
    private int pageSize = int.Parse(Resources.Resource.pagenumber);

    protected void Page_Load(object sender, EventArgs e)
    {
        LoginModel loginModel = (LoginModel)Session["loginModel"];

        if (loginModel == null)
        {
            Response.Redirect("../login.aspx");
        }
        else if ( loginModel.isKumiaiin())
        {
            Response.Redirect("../menu.aspx");
        }

        if (loginModel.isKumiaiin() && sessioncontrol.control())
        {
            Response.Redirect("../control.aspx");
        }

        if (loginModel.isJimukyoku())
        {
            oldtime.Visible = true;
        }
        else
        {
            //oldtime.Visible = false;
        }

        if (!IsPostBack)
        {
            table.Visible = false;
            none.Visible = false;

            REPORT_DATE_STR.Text = System.DateTime.Now.ToString("yyyyMMdd");
            REPORT_DATE_END.Text = System.DateTime.Now.ToString("yyyyMMdd");

            

            string area = Resources.Resource.area;
            string[] areaArray = area.Split(',');
            for (int i = 0; i < areaArray.Length; i++)
            {
                ListItem item = new ListItem();
                item.Value = (i + 1) + "";
                item.Text = areaArray[i];
                HALL_PREF_CODE.Items.Insert(i + 1, item);
            }

            if (Session["back"] != null && (int.Parse(Session["back"].ToString()) == 1))
            {
                ReportSearchModel M = (ReportSearchModel)Session["searchReportModel"];
                REPORT_DATE_STR.Text = M.getReport_date_str();
                REPORT_DATE_END.Text = M.getReport_date_end();
                UNION_CODE.Text = M.getUnion_code();
                OFFICE_NAME.Text = M.getOffice_name();
                OFFICE_KANA.Text = M.getOffice_kana();
                BRANCH_NAME.Text = M.getBranch_name();
                SEAL_STR_NO.Text = M.getSeal_str_no();
                SEAL_END_NO.Text = M.getSeal_end_no();
                HALL_NAME.Text = M.getHall_name();
                if (M.getHall_pref_code() > 0)
                {
                    HALL_PREF_CODE.Text = M.getHall_pref_code().ToString();
                }
                HALL_CITY.Text = M.getHall_city();
                TRADER_REG_NO1.Text = M.getTrader_reg_no1();
                TRADER_REG_NO2.Text = M.getTrader_reg_no2();
                TRADER_NAME.Text = M.getTrader_name();
                TRADER_KANA.Text = M.getTrader_kana();
                EST_DATE_STR.Text = M.getEst_date_str();
                EST_DATE_END.Text = M.getEst_date_end();
                CHIEF_NO1.Text = M.getChief_no1();
                CHIEF_NO2.Text = M.getChief_no2();
                CHIEF_NAME.Text = M.getChief_name();
                
                if ("20".Equals(M.getReport_clss()))
                {
                    REPORT_CLSS2.Checked = true;
                }
                else if ("10".Equals(M.getReport_clss()))
                {
                    REPORT_CLSS3.Checked = true;
                }
                else
                {
                    REPORT_CLSS1.Checked = true;
                }
               

                if ("10".Equals(M.getReport_sts()))
                {
                    REPORT_STS2.Checked = true;
                }
                else if ("05".Equals(M.getReport_sts()))
                {
                    REPORT_STS3.Checked = true;
                }
                else if ("888".Equals(M.getReport_sts()))
                {
                    REPORT_STS4.Checked = true;
                }
                else if (M.getCansel())
                {
                    REPORT_STS5.Checked = true;
                }
                else{
                    REPORT_STS1.Checked = true;
                }

                REPORT_NO.Text = M.getReport_no();

                if ("1".Equals(M.getMachine_clss()))
                {
                    MS2.Checked = true;
                }
                else if ("2".Equals(M.getMachine_clss()))
                {
                    MS3.Checked = true;
                }
                else
                {
                    REPORT_STS1.Checked = true;
                }

                if (loginModel.isJimukyoku())
                {
                    if (M.getOldtime())
                    {
                        oldtime.Checked = true;
                    }
                    
                }

                page(int.Parse(Session["pageNo"].ToString()), true);

                Session.Remove("back");
            }

        }

    }

    private void page(int no, bool back)
    {
        try
        {
            LoginModel loginModel = (LoginModel)Session["loginModel"];
            //カプセル化オブジェクト
            ReportSearchModel M = new ReportSearchModel();
            if (!back)
            {
                M.setReport_date_str(REPORT_DATE_STR.Text.Trim().Length == 8 ? REPORT_DATE_STR.Text.Trim() : null);
                M.setReport_date_end(REPORT_DATE_END.Text.Trim().Length == 8 ? REPORT_DATE_END.Text.Trim() : null);

                
                if (loginModel.isJimukyoku() && !oldtime.Checked)
                {
                    if (M.getReport_date_str() == null)
                    {
                        M.setReport_date_str((System.DateTime.Now.Year - 4) + "0101");
                    }
                    else if (M.getReport_date_str().CompareTo((System.DateTime.Now.Year - 4) + "0101") < 0)
                    {
                        M.setReport_date_str((System.DateTime.Now.Year - 4) + "0101");
                    }
                   
                }

                if (loginModel.isJimukyoku())
                {
                    M.setOldtime(oldtime.Checked);
                }
                M.setUnion_code(UNION_CODE.Text);
                M.setOffice_name(OFFICE_NAME.Text);
                M.setOffice_kana(OFFICE_KANA.Text);
                M.setBranch_name(BRANCH_NAME.Text);
                M.setSeal_str_no(SEAL_STR_NO.Text.Trim().Length != 0 ? SEAL_STR_NO.Text.Trim() : null);
                M.setSeal_end_no(SEAL_END_NO.Text.Trim().Length != 0 ? SEAL_END_NO.Text.Trim() : null);
                M.setHall_name(HALL_NAME.Text);
                M.setHall_pref_code(int.Parse(HALL_PREF_CODE.Text));
                M.setHall_city(HALL_CITY.Text);
                M.setTrader_reg_no1(TRADER_REG_NO1.Text);
                M.setTrader_reg_no2(TRADER_REG_NO2.Text);
                //M.setTrader_reg_no(TRADER_REG_NO1.Text + TRADER_REG_NO2.Text);
                M.setTrader_name(TRADER_NAME.Text);
                M.setTrader_kana(TRADER_KANA.Text);
                M.setEst_date_str(EST_DATE_STR.Text);
                M.setEst_date_end(EST_DATE_END.Text);
                M.setChief_no1(CHIEF_NO1.Text);
                M.setChief_no2(CHIEF_NO2.Text);
                M.setChief_name(CHIEF_NAME.Text);
                if (REPORT_CLSS2.Checked)
                {
                    M.setReport_clss("20");
                    
                }
                else if (REPORT_CLSS3.Checked)
                {
                    M.setReport_clss("10");
                }
                
                if (REPORT_STS2.Checked){ M.setReport_sts("10");}
                else if (REPORT_STS3.Checked) { M.setReport_sts("05");}
                else if (REPORT_STS4.Checked) { M.setReport_sts("888"); }
                else if (REPORT_STS5.Checked) { M.setCansel(true); M.setReport_sts("111"); }
                else M.setReport_sts(null);

               // M.setReport_err(REPORT_STS2.Checked ? "20" : REPORT_STS3.Checked ? "10" : null);
                M.setReport_err(REPORT_ERR.Checked ? "1" : null);
                M.setReport_no(REPORT_NO.Text.Trim().Length != 0 ? REPORT_NO.Text.Trim() : null);
                M.setMachine_clss(MS2.Checked ? "1" : MS3.Checked ? "2" : null);


                //------------------------------------------validation--------------------------------------
                InputErrorException ie = new InputErrorException(Resources.Resource.ResourceManager);
                if (!StringUtil.isEmpty(REPORT_DATE_STR.Text))
                {
                    if (!StringUtil.isNumber(REPORT_DATE_STR.Text))
                    {
                        ie.addErrorCode("E9004");
                    }

                    if (REPORT_DATE_STR.Text.Length < 8)
                    {
                        ie.addErrorCode("E9004");
                    }
                }

                if (!StringUtil.isEmpty(REPORT_DATE_END.Text))
                {
                    if (!StringUtil.isNumber(REPORT_DATE_END.Text))
                    {
                        ie.addErrorCode("E9004");
                    }

                    if (REPORT_DATE_END.Text.Length < 8)
                    {

                        ie.addErrorCode("E9004");
                    }
                }

                if (!StringUtil.isEmpty(SEAL_STR_NO.Text))
                {
                    if (!StringUtil.isNumber(SEAL_STR_NO.Text))
                    {
                        ie.addErrorCode("E9005");
                    }

                    if (SEAL_STR_NO.Text.Length < 9)
                    {

                        ie.addErrorCode("E9005");
                    }
                }

                if (!StringUtil.isEmpty(SEAL_END_NO.Text))
                {
                    if (!StringUtil.isNumber(SEAL_END_NO.Text))
                    {
                        ie.addErrorCode("E9005");
                    }

                    if (SEAL_END_NO.Text.Length < 9)
                    {

                        ie.addErrorCode("E9005");
                    }
                }


                if (!StringUtil.isEmpty(REPORT_NO.Text))
                {
                    Regex r = new Regex(@"^[a-zA-Z]{1}\d{9}$");
                    if (!r.IsMatch(REPORT_NO.Text))
                    {
                        ie.addErrorCode("E9006");
                    }

                }
                List<string> errorCodeList = ie.getErrorMessages();
                msg.Text = ErrorCodeUtil.errorCodeListToHtml(errorCodeList);

                if (errorCodeList.Count > 0)
                {
                    table.Visible = false;
                    none.Visible = true;
                    return;
                }
                //------------------------------------------validation--------------------------------------


            }
            else
            {
                M = (ReportSearchModel)Session["searchReportModel"];

            }

            Session["searchReportModel"] = M;
            Session["pageNo"] = no;

            TRN_Report_H_Persister dao = new TRN_Report_H_Persister();
            if (no <= 0) no = 1;
            Pager pager = dao.search(M, no, pageSize);
            
            //if (no > pager.Pages) no = pager.Pages;
            //pager = dao.search(M, no, pageSize);
            pageNo.Value = no.ToString();
            pages.Text = "全" + pager.Total + "件／"+pager.Other+"台";
            ArrayList list = pager.List;

            if (list.Count == 0)
            {
                table.Visible = false;
                none.Visible = true;
            }
            else
            {
                none.Visible = false;
                table.Visible = true;

                //datatable.Rows.Clear();
                if (no == 1)
                {
                    prebutton.Visible = false;
                }
                else
                {
                    prebutton.Visible = true;
                    prebutton.Text = "≪ 前の" + pageSize + "件";

                }

                if (no == pager.Pages)
                {
                    nextbutton.Visible = false;

                }
                else
                {
                    nextbutton.Visible = true;
                    nextbutton.Text = " 次の" + pageSize + "件 ≫";
                }

                MST_UnionPersister unionDao = new MST_UnionPersister();
                MST_Union_BranchPersister branchDao = new MST_Union_BranchPersister();
                MST_TraderPersister traderDao = new MST_TraderPersister();
                TRN_Report_D_Persister reportD = new TRN_Report_D_Persister();

                string area = Resources.Resource.area;
                string[] areaArray = area.Split(',');

                int totalms = 0;
                for (int i = 0; i < list.Count; i++)
                // foreach (object o in list)
                {
                    ReportSearchModel mu = (ReportSearchModel)list[i];
                    totalms = mu.getMachine_qty() + totalms;
                    MstTrader tradermodel = traderDao.getDetail(mu.getTrader_reg_no() == null ? "" : mu.getTrader_reg_no1()+mu.getTrader_reg_no2());
                    MstUnion unionmodel = unionDao.getDetail(mu.getUnion_code() == null ? "" : mu.getUnion_code());
                    mst_union_branch branchmodel = branchDao.getDetail(mu.getUnion_code() == null ? "" : mu.getUnion_code(), mu.getBranch_code());

                    ArrayList reportDList = reportD.get(mu.getReport_no());

                    TableRow row = new TableRow();
                    if (mu.isError())
                    {
                        row.ForeColor = Color.Red;
                    }

                    TableCell td = new TableCell();
                    td.BackColor = Color.White;
                    td.Height = 30;
                    td.HorizontalAlign = HorizontalAlign.Center;
                    if (REPORT_STS4.Checked || "90".Equals(mu.getReport_sts()) || StringUtil.isEmpty(mu.getReport_clss()))
                    {
                        td.Text = "";
                    }
                    else
                    {
                        td.Text = mu.getReport_date().ToString("yyyy/MM/dd");
                    }
                    row.Cells.Add(td);

                    TrnReportd model_d = null;
                    StringBuilder sb1 = new StringBuilder();

                    for (int j = 0; j < reportDList.Count; j++)
                    {
                        model_d = (TrnReportd)reportDList[j];
                        if (StringUtil.isEmpty(model_d.SealEndNo))
                        {
                            sb1.Append(model_d.SealStrNo +  "<br/>");
                        }
                        else
                        {
                            sb1.Append(model_d.SealStrNo + " ～ " + model_d.SealEndNo + "<br/>");
                        }
                    }
                    td = new TableCell();
                    td.BackColor = Color.White;
                    td.Height = 30;
                    td.HorizontalAlign =HorizontalAlign.Left ;
                    if (StringUtil.isEmpty(mu.getReport_clss()))
                    {
                        td.Text = mu.getReport_no();
                    }
                    else
                    {
                        td.Text =  sb1.ToString();
                    }
                    row.Cells.Add(td);

                    StringBuilder sb2 = new StringBuilder();
                    if (unionmodel != null)
                    {
                        sb2.Append(unionmodel.OfficeName + "<br/>");
                        if (branchmodel != null)
                        {
                            sb2.Append(branchmodel.officename);
                        }
                    }

                    td = new TableCell();
                    td.BackColor = Color.White;
                    td.Height = 30;
                   // td.Text = StringUtil.isEmpty(mu.getReport_clss()) ? "" : sb2.ToString();
                    td.Text =  sb2.ToString();
                    row.Cells.Add(td);

                    StringBuilder sb3 = new StringBuilder();
                    sb3.Append(mu.getHall_name() + "<br/>");
                    sb3.Append(mu.getHall_pref_code() != 0 ? areaArray[mu.getHall_pref_code() - 1] : "");
                    sb3.Append("<br/>");
                    sb3.Append(mu.getHall_city() + "<br/>");
                    sb3.Append(mu.getHall_address());

                    td = new TableCell();
                    td.BackColor = Color.White;
                    td.Height = 30;
                    td.HorizontalAlign = HorizontalAlign.Left;
                    td.Text = StringUtil.isEmpty(mu.getReport_clss()) ? "" : sb3.ToString();
                    row.Cells.Add(td);


                    td = new TableCell();
                    td.BackColor = Color.White;
                    td.Height = 30;
                    td.HorizontalAlign = HorizontalAlign.Left;
                    if (StringUtil.isEmpty(mu.getReport_clss()))
                    {
                        td.Text = "";
                    }
                    else
                    {
                        td.Text =((mu.getMachine_name()) + "<br/>" + (tradermodel == null ? "" : tradermodel.TraderName));
                    }
                    row.Cells.Add(td);


                    td = new TableCell();
                    td.BackColor = Color.White;
                    td.Height = 30;
                    td.HorizontalAlign = HorizontalAlign.Center;
                    if (StringUtil.isEmpty(mu.getReport_clss()))
                    {
                        td.Text = "";
                    }
                    else
                    {
                        td.Text = StringUtil.isEmpty(mu.getReport_clss()) ? "" : (mu.getEst_date() == null ? "" : mu.getEst_date().ToString("yyyy/MM/dd"));
                    }
                    row.Cells.Add(td);

                    td = new TableCell();
                    td.BackColor = Color.White;
                    td.Height = 30;
                    td.HorizontalAlign = HorizontalAlign.Right;
                    td.Text = mu.getMachine_qty().ToString();
                    row.Cells.Add(td);

                    td = new TableCell();
                    td.BackColor = Color.White;
                    td.Height = 30;
                    td.HorizontalAlign = HorizontalAlign.Center;
                    if (M.getCansel() || REPORT_STS4.Checked || "90".Equals(mu.getReport_sts()) || StringUtil.isEmpty(mu.getReport_clss()))
                    {
                        td.Text = "";
                    }
                    else
                    {
                        td.Text = TrnReporth.CLSS_INTERNET.Equals(mu.getReport_clss()) ? "IN" : "FAX";
                    }
                    row.Cells.Add(td);

                    td = new TableCell();
                    td.BackColor = Color.White;
                    td.Height = 30;
                    td.HorizontalAlign = HorizontalAlign.Center;
                    if (StringUtil.isEmpty(mu.getReport_clss()))
                    {
                        td.Text = "";
                    }
                    else
                    {
                        td.Text = mu.getReport_no() == null ? "" : mu.getReport_no();
                    }
                    row.Cells.Add(td);

                    

                    td = new TableCell();
                    td.BackColor = Color.White;
                    td.Height = 30;
                    td.HorizontalAlign = HorizontalAlign.Center;
                    if (M.getCansel())
                    {
                        td.Text = "破棄";
                    }else if (StringUtil.isEmpty(mu.getReport_clss()) && mu.getReport_sts().Equals("40"))
                    {
                        td.Text = "報告済";
                    }
                    else if (StringUtil.isEmpty(mu.getReport_clss()) && mu.getReport_sts().Equals("10") || REPORT_STS4.Checked)
                    {
                        td.Text = "未報告" ;
                    }
                    else if (StringUtil.isEmpty(mu.getReport_clss()) && mu.getReport_sts().Equals("30"))
                    {
                        td.Text = "未承認";
                    }
                    else 
                    {
                        td.Text = mu.getReport_sts_mozi();
                    }
                    row.Cells.Add(td);

                    
                    td = new TableCell();
                    td.BackColor = Color.White;
                    td.Height = 30;
                    td.HorizontalAlign = HorizontalAlign.Center;
                    
                    td.Text = mu.isError()?"ERR":"";
                    row.Cells.Add(td);
                    

                    td = new TableCell();
                    td.BackColor = Color.White;
                    td.Height = 30;
                    td.HorizontalAlign = HorizontalAlign.Center;
                    td.Text = StringUtil.isEmpty(mu.getReport_clss()) ? "" : "<a href='seal_report_detail.aspx?report_no=" + mu.getReport_no() + "'>表示</a>";
                    row.Cells.Add(td);

                    datatable.Rows.Add(row);
                }
                /*
                TableRow row2 = new TableRow();
                TableCell td2 = new TableCell();
                td2.BackColor = Color.White;
                td2.Height = 30;
                row2.Cells.Add(td2);
                td2 = new TableCell();
                td2.BackColor = Color.White;
                td2.Height = 30;
                row2.Cells.Add(td2);

                td2 = new TableCell();
                td2.BackColor = Color.White;
                td2.Height = 30;
                row2.Cells.Add(td2);
                td2 = new TableCell();
                td2.BackColor = Color.White;
                td2.Height = 30;
                row2.Cells.Add(td2);
                td2 = new TableCell();
                td2.BackColor = Color.White;
                td2.Height = 30;
                row2.Cells.Add(td2);
                row2.Cells.Add(td2);
                td2 = new TableCell();
                td2.BackColor = Color.White;
                td2.Height = 30;

                row2.Cells.Add(td2);
                td2 = new TableCell();
                td2.BackColor = Color.White;
                td2.Height = 30;
                td2.HorizontalAlign = HorizontalAlign.Right;
                td2.Text = totalms.ToString();
                row2.Cells.Add(td2);
                td2 = new TableCell();
                td2.BackColor = Color.White;
                td2.Height = 30;
                row2.Cells.Add(td2);
                td2 = new TableCell();
                td2.BackColor = Color.White;
                td2.Height = 30;
                row2.Cells.Add(td2);
                td2 = new TableCell();
                td2.BackColor = Color.White;
                td2.Height = 30;
                row2.Cells.Add(td2);
                datatable.Rows.Add(row2);
                */
            }
        }catch(Exception e){

        }
    }

    protected void pre(object sender, EventArgs e)
    {
        Session.Remove("back");
        Session.Remove("searchReportModel");
        Session.Remove("pageNo");
        page(int.Parse(pageNo.Value) - 1, false);
    }

    protected void next(object sender, EventArgs e)
    {
        Session.Remove("back");
        Session.Remove("searchReportModel");
        Session.Remove("pageNo");
        page(int.Parse(pageNo.Value) + 1, false);
    }

    protected void search(object sender, EventArgs e)
    {
        Session.Remove("back");
        Session.Remove("searchReportModel");
        Session.Remove("pageNo");
        page(1, false);


    }
}
